package jEDF.EDF;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

import jEDF.JEDF.*;

/**
 *
 * <p>Titre : jEDF</p>
 * <p>Description : Java European Data Format Viewer and Analyser</p>
 * <p>Author : Nizar Kerkeni</p>
 * <p>Copyright : Copyright (c) 2003-2006</p>
 * <p>Version : 2.0</p>
 */

public class EDFInformationDialog extends JDialog {
    private JPanel jPanelOkBtn = new JPanel();
    private BorderLayout borderLayout1 = new BorderLayout();
    private JButton jButtonOK = new JButton();
    private JPanel jPanelHeader = new JPanel();
    private JPanel jPanelTableInfos = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private JTextField jTextFieldVersion = new JTextField();
    private JLabel jLabel2 = new JLabel();
    private JTextField jTextFieldPatientID = new JTextField();
    private JLabel jLabel3 = new JLabel();
    private JTextField jTextFieldRecordID = new JTextField();
    private JLabel jLabel4 = new JLabel();
    private JTextField jTextFieldStDate = new JTextField();
    private JTextField jTextFieldTotalDuration = new JTextField();
    private JLabel jLabel6 = new JLabel();
    private JTextField jTextFieldNumRecord = new JTextField();
    private JTextField jTextFieldDuration = new JTextField();
    private JTextField jTextFieldNumSignals = new JTextField();
    private JLabel jLabel7 = new JLabel();
    private JLabel jLabel8 = new JLabel();
    private JLabel jLabel9 = new JLabel();
    private JTextField jTextFieldFile = new JTextField();
    private JLabel jLabel10 = new JLabel();
    private BorderLayout borderLayout2 = new BorderLayout();
    private JTable jTable1;
    private JScrollPane scrollPane1;

    private EDFFile edfFile;

    public EDFInformationDialog(JEDFMainWindow mainWindow) {
        try {
            jbInit();
            this.edfFile = mainWindow.getEDFFile();
            this.setDialogFildes(edfFile);
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * setDialogFildes
     *
     * @param edfFile EDFFile
     */
    private void setDialogFildes(EDFFile edfFile) {
        Calendar startCalendar = edfFile.getStartTime();
        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        startCalendar.add(Calendar.SECOND, edfFile.getTotalDuration());
        this.jTextFieldVersion.setText(edfFile.getVersion().trim());
        this.jTextFieldFile.setText(edfFile.getFileName().trim());
        this.jTextFieldPatientID.setText(edfFile.getPatientID());
        this.jTextFieldRecordID.setText(edfFile.getRecordID());
        this.jTextFieldStDate.setText(dateFormat.format(startCalendar.getTime()));
        startCalendar.clear();
        startCalendar.add(Calendar.SECOND, edfFile.getTotalDuration());
        dateFormat = DateFormat.getTimeInstance();
        this.jTextFieldTotalDuration.setText(dateFormat.format(startCalendar.getTime()));

        this.jTextFieldNumSignals.setText(new Integer(edfFile.getNbSignals()).toString());
        this.jTextFieldNumRecord.setText(new Long(edfFile.getNbRecords()).toString());
        this.jTextFieldDuration.setText(new Long(edfFile.getRecordDuration()).toString());
        this.jTable1 = new JTable(new SignalInformations());
        scrollPane1 = new JScrollPane(jTable1);
        jPanelTableInfos.add(scrollPane1, BorderLayout.CENTER);
    }

    private void jbInit()
            throws Exception {
        this.setTitle(JEDFMainWindow.getLocalizedString("EDF file informations"));
        this.getContentPane().setLayout(borderLayout1);
        jPanelOkBtn.setMinimumSize(new Dimension(700, 35));
        jPanelOkBtn.setPreferredSize(new Dimension(700, 35));
        jButtonOK.setMaximumSize(new Dimension(55, 23));
        jButtonOK.setMinimumSize(new Dimension(55, 23));
        jButtonOK.setMnemonic('0');
        jButtonOK.setText(JEDFMainWindow.getLocalizedString("OK"));
        jButtonOK.addActionListener(new EDFInformationDialog_jButtonOK_actionAdapter(this));
        jPanelHeader.setBorder(BorderFactory.createEtchedBorder());
        jPanelHeader.setMinimumSize(new Dimension(700, 130));
        jPanelHeader.setPreferredSize(new Dimension(650, 130));
        jPanelHeader.setLayout(null);
        jLabel1.setText(JEDFMainWindow.getLocalizedString("EDF version"));
        jLabel1.setBounds(new Rectangle(15, 16, 84, 17));
        jTextFieldVersion.setBackground(Color.white);
        jTextFieldVersion.setMinimumSize(new Dimension(60, 17));
        jTextFieldVersion.setPreferredSize(new Dimension(60, 17));
        jTextFieldVersion.setEditable(false);
        jTextFieldVersion.setMargin(new Insets(0, 5, 0, 0));
        jTextFieldVersion.setBounds(new Rectangle(100, 16, 66, 17));
        jLabel2.setText(JEDFMainWindow.getLocalizedString("Patient Identification"));
        jLabel2.setBounds(new Rectangle(15, 37, 143, 17));
        jTextFieldPatientID.setBackground(Color.white);
        jTextFieldPatientID.setMinimumSize(new Dimension(500, 17));
        jTextFieldPatientID.setPreferredSize(new Dimension(500, 17));
        jTextFieldPatientID.setEditable(false);
        jTextFieldPatientID.setMargin(new Insets(0, 5, 0, 0));
        jTextFieldPatientID.setBounds(new Rectangle(156, 37, 534, 17));
        jLabel3.setBounds(new Rectangle(15, 57, 143, 17));
        jLabel3.setText(JEDFMainWindow.getLocalizedString("Record Identification"));
        jTextFieldRecordID.setBounds(new Rectangle(156, 57, 534, 17));
        jTextFieldRecordID.setEditable(false);
        jTextFieldRecordID.setMargin(new Insets(0, 5, 0, 0));
        jTextFieldRecordID.setPreferredSize(new Dimension(550, 17));
        jTextFieldRecordID.setBackground(Color.white);
        jTextFieldRecordID.setMinimumSize(new Dimension(550, 17));
        jLabel4.setBounds(new Rectangle(15, 78, 140, 17));
        jLabel4.setText(JEDFMainWindow.getLocalizedString("Recording Start Date"));
        jTextFieldStDate.setBounds(new Rectangle(156, 78, 234, 17));
        jTextFieldStDate.setEditable(false);
        jTextFieldStDate.setMargin(new Insets(0, 3, 0, 0));
        jTextFieldStDate.setPreferredSize(new Dimension(60, 17));
        jTextFieldStDate.setMinimumSize(new Dimension(60, 17));
        jTextFieldStDate.setBackground(Color.white);
        jTextFieldTotalDuration.setBackground(Color.white);
        jTextFieldTotalDuration.setMinimumSize(new Dimension(60, 17));
        jTextFieldTotalDuration.setPreferredSize(new Dimension(60, 17));
        jTextFieldTotalDuration.setEditable(false);
        jTextFieldTotalDuration.setMargin(new Insets(0, 3, 0, 0));
        jTextFieldTotalDuration.setBounds(new Rectangle(530, 78, 160, 17));
        jLabel6.setText(JEDFMainWindow.getLocalizedString("Recording Duration"));
        jLabel6.setBounds(new Rectangle(397, 78, 132, 17));
        jTextFieldNumRecord.setBounds(new Rectangle(584, 98, 106, 17));
        jTextFieldNumRecord.setEditable(false);
        jTextFieldNumRecord.setMargin(new Insets(0, 3, 0, 0));
        jTextFieldNumRecord.setPreferredSize(new Dimension(60, 17));
        jTextFieldNumRecord.setMinimumSize(new Dimension(60, 17));
        jTextFieldNumRecord.setBackground(Color.white);
        jTextFieldDuration.setBackground(Color.white);
        jTextFieldDuration.setMinimumSize(new Dimension(60, 17));
        jTextFieldDuration.setPreferredSize(new Dimension(60, 17));
        jTextFieldDuration.setEditable(false);
        jTextFieldDuration.setMargin(new Insets(0, 3, 0, 0));
        jTextFieldDuration.setBounds(new Rectangle(351, 98, 104, 17));
        jTextFieldNumSignals.setBackground(Color.white);
        jTextFieldNumSignals.setMinimumSize(new Dimension(60, 17));
        jTextFieldNumSignals.setPreferredSize(new Dimension(60, 17));
        jTextFieldNumSignals.setEditable(false);
        jTextFieldNumSignals.setMargin(new Insets(0, 3, 0, 0));
        jTextFieldNumSignals.setBounds(new Rectangle(140, 98, 82, 17));
        jLabel7.setBounds(new Rectangle(459, 98, 121, 17));
        jLabel7.setText(JEDFMainWindow.getLocalizedString("Number of Epochs"));
        jLabel8.setText(JEDFMainWindow.getLocalizedString("Epoch Duration (s)"));
        jLabel8.setBounds(new Rectangle(227, 98, 122, 17));
        jLabel9.setText(JEDFMainWindow.getLocalizedString("Number of signals"));
        jLabel9.setBounds(new Rectangle(15, 98, 127, 17));
        jTextFieldFile.setBounds(new Rectangle(231, 16, 459, 17));
        jTextFieldFile.setEditable(false);
        jTextFieldFile.setMargin(new Insets(0, 5, 0, 0));
        jTextFieldFile.setPreferredSize(new Dimension(60, 17));
        jTextFieldFile.setMinimumSize(new Dimension(60, 17));
        jTextFieldFile.setBackground(Color.white);
        jLabel10.setBounds(new Rectangle(169, 16, 60, 17));
        jLabel10.setText(JEDFMainWindow.getLocalizedString("EDF file"));
        jPanelTableInfos.setMinimumSize(new Dimension(700, 200));
        jPanelTableInfos.setPreferredSize(new Dimension(700, 200));
        jPanelTableInfos.setLayout(borderLayout2);
        jPanelOkBtn.add(jButtonOK, null);
        jPanelHeader.add(jLabel1, null);
        jPanelHeader.add(jTextFieldVersion, null);
        jPanelHeader.add(jLabel2, null);
        jPanelHeader.add(jTextFieldPatientID, null);
        jPanelHeader.add(jLabel3, null);
        jPanelHeader.add(jTextFieldRecordID, null);
        jPanelHeader.add(jLabel4, null);
        jPanelHeader.add(jTextFieldStDate, null);
        jPanelHeader.add(jLabel6, null);
        jPanelHeader.add(jTextFieldTotalDuration, null);
        jPanelHeader.add(jTextFieldNumRecord, null);
        jPanelHeader.add(jLabel9, null);
        jPanelHeader.add(jTextFieldNumSignals, null);
        jPanelHeader.add(jLabel8, null);
        jPanelHeader.add(jTextFieldDuration, null);
        jPanelHeader.add(jLabel7, null);
        jPanelHeader.add(jTextFieldFile, null);
        jPanelHeader.add(jLabel10, null);
        this.getContentPane().add(jPanelOkBtn, BorderLayout.SOUTH);
        this.getContentPane().add(jPanelTableInfos, BorderLayout.CENTER);
        this.getContentPane().add(jPanelHeader, BorderLayout.NORTH);
    }

    public void jButtonOK_actionPerformed(ActionEvent e) {
        this.dispose();
    }


    private class SignalInformations extends AbstractTableModel {

        private String[] columnNames = {
                               "N°",
                               JEDFMainWindow.getLocalizedString("Label"),
                               JEDFMainWindow.getLocalizedString("Transducer type"),
                               JEDFMainWindow.getLocalizedString("Physical dimension"),
                               JEDFMainWindow.getLocalizedString("Physical min"),
                               JEDFMainWindow.getLocalizedString("Physical max"),
                               JEDFMainWindow.getLocalizedString("Digital min"),
                               JEDFMainWindow.getLocalizedString("Digital max"),
                               JEDFMainWindow.getLocalizedString("Prefiltering"),
                               JEDFMainWindow.getLocalizedString("Number of samples")};

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getRowCount()
         */
        public int getRowCount() {
            return edfFile.getNbSignals();
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getColumnCount()
         */
        public int getColumnCount() {
            return columnNames.length;
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getValueAt(int, int)
         */
        public Object getValueAt(int arg0, int arg1) {
            EDFSignal signal = edfFile.getSignal(arg0);
            switch (arg1) {
                case 0:
                    return new Integer(arg0);
                case 1:
                    return signal.getLabel();
                case 2:
                    return signal.getTransducerType();
                case 3:
                    return signal.getPhysDim();
                case 4:
                    return new Double(signal.getPhysMin());
                case 5:
                    return new Double(signal.getPhysMax());
                case 6:
                    return new Integer(signal.getDigMin());
                case 7:
                    return new Integer(signal.getDigMax());
                case 8:
                    return signal.getPrefiltering();
                case 9:
                    return new Integer(signal.getNbSamples());
            }
            return "";
        }

        /* (non-Javadoc)
         * @see javax.swing.table.AbstractTableModel#getColumnName(int)
         */
        public String getColumnName(int arg0) {
            return columnNames[arg0];
        }


    }


    class EDFInformationDialog_jButtonOK_actionAdapter implements ActionListener {
        private EDFInformationDialog adaptee;
        EDFInformationDialog_jButtonOK_actionAdapter(EDFInformationDialog adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent e) {
            adaptee.jButtonOK_actionPerformed(e);
        }
    }
}
